﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.Helper;
using System.IO;
using ImplementationInitialization;

namespace LDSP.UI.Admin
{
    public partial class ImportChemical : BRL.BaseSysAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                string folderName = UIHelper.GetTempFolder();
                string fileName = Path.Combine(folderName, Guid.NewGuid().ToString() + Path.GetExtension(this.fup_file.FileName));
                this.fup_file.SaveAs(fileName);
                this.Import(fileName);
                File.Delete(fileName);
                this.JSAlert("导入成功！");
            }
            catch (Exception ex)
            {
                this.JSAlert("导入失败！");
            }
        }

        protected void Import(string fileName)
        {
            ChemicalImport import = new ChemicalImport();
            import.Run(fileName);

            this.grd_list.DataSource = import.ImportResult.ChemicalResult;
            this.grd_list.DataBind();
        }

        protected void grd_list_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    this.ShowSequenceNumber(e.Row, 1);
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected void btn_template_download_Click(object sender, EventArgs e)
        {
            DownloadFile("~/Templates/chemicalImport.xls");
        }
    }
}